package com.ruoyi.charging.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.ruoyi.charging.common.GlobalConstant;
import com.ruoyi.charging.domain.ChargingStationVO;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.*;
import org.springframework.data.redis.connection.RedisGeoCommands;
import org.springframework.data.redis.core.BoundGeoOperations;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.ruoyi.charging.mapper.ChargingStationMapper;
import com.ruoyi.charging.domain.ChargingStation;
import com.ruoyi.charging.service.IChargingStationService;

/**
 * 充电站Service业务层处理
 * 
 * @author tedu
 * @date 2023-12-18
 */
@Service
public class ChargingStationServiceImpl implements IChargingStationService 
{
    @Autowired(required = false)
    private ChargingStationMapper chargingStationMapper;

    @Autowired
    RedisTemplate redisTemplate;

    @Override
    public List<ChargingStationVO> selectNearbyStation(Double longitude, Double latitude, Double radius) {
        ArrayList<ChargingStationVO> list = new ArrayList<>();
        BoundGeoOperations boundGeoOperations = redisTemplate.boundGeoOps
                (GlobalConstant.STATION_GEO_ZSET);
        //1.创建查找中心点
        Point center = new Point(longitude, latitude);

        //2.创建查找半径
        Distance distance = new Distance(radius, Metrics.KILOMETERS);

        //Distance(double value,Metric metric)
        //3.创建查找范围
        Circle circle = new Circle(center, distance);
        // Circle(Point center,Distance radius)
        //4.设置查找参数
        RedisGeoCommands.GeoRadiusCommandArgs geoRadiusCommandArgs = RedisGeoCommands
                .GeoRadiusCommandArgs.newGeoRadiusArgs();
        //4.1 设置返回的位置信息包括距离
        geoRadiusCommandArgs.includeDistance();
        //4.2 包括坐标
        geoRadiusCommandArgs.includeCoordinates();
        //5.到redis中查找
        GeoResults<RedisGeoCommands.GeoLocation<String>> geoResults = boundGeoOperations.radius(circle, geoRadiusCommandArgs);
        //radius(Circle within,GeoRadiusCommandArgs args)
        //6.得到迭代器
        Iterator<GeoResult<RedisGeoCommands.GeoLocation<String>>> iterator = geoResults
                .iterator();
        BoundHashOperations boundHashOperations = redisTemplate.boundHashOps
                (GlobalConstant.STATION_DETAIL_HASH);
        //7.遍历迭代器
        while (iterator.hasNext()) {
            //7.1 取代迭代器中的对象
            GeoResult<RedisGeoCommands.GeoLocation<String>> geoResult = iterator.next();
            //7.2 取充电站编号
            String stationId = geoResult.getContent().getName();
            //7.3 根据编号取充电站详情
            ChargingStation chargingStation = (ChargingStation) boundHashOperations
                    .get(stationId);
            //7.4 取距离
            double stationDistance = geoResult.getDistance().getValue();
            //7.5 创建vo
            ChargingStationVO chargingStationVO = new ChargingStationVO();
            //7.6 把充电站详情copy到vo中
            BeanUtils.copyProperties(chargingStation,chargingStationVO);
            //7.7 设计vo的距离
            chargingStationVO.setDistance(stationDistance);
            //7.8 把vo放到集合中
            list.add(chargingStationVO);
        }
        return list;
    }

    /**
     * 查询充电站
     * 
     * @param id 充电站主键
     * @return 充电站
     */
    @Override
    public ChargingStation selectChargingStationById(Integer id)
    {
        return chargingStationMapper.selectChargingStationById(id);
    }

    /**
     * 查询充电站列表
     * 
     * @param chargingStation 充电站
     * @return 充电站
     */
    @Override
    public List<ChargingStation> selectChargingStationList(ChargingStation chargingStation)
    {
        return chargingStationMapper.selectChargingStationList(chargingStation);
    }

    /**
     * 新增充电站
     * 
     * @param chargingStation 充电站
     * @return 结果
     */
    @Override
    public int insertChargingStation(ChargingStation chargingStation)
    {
        chargingStation.setCreateTime(DateUtils.getNowDate());
        return chargingStationMapper.insertChargingStation(chargingStation);
    }

    /**
     * 修改充电站
     * 
     * @param chargingStation 充电站
     * @return 结果
     */
    @Override
    public int updateChargingStation(ChargingStation chargingStation)
    {
        chargingStation.setUpdateTime(DateUtils.getNowDate());
        return chargingStationMapper.updateChargingStation(chargingStation);
    }

    /**
     * 批量删除充电站
     * 
     * @param ids 需要删除的充电站主键
     * @return 结果
     */
    @Override
    public int deleteChargingStationByIds(Integer[] ids)
    {
        return chargingStationMapper.deleteChargingStationByIds(ids);
    }

    /**
     * 删除充电站信息
     * 
     * @param id 充电站主键
     * @return 结果
     */
    @Override
    public int deleteChargingStationById(Integer id)
    {
        return chargingStationMapper.deleteChargingStationById(id);
    }
}
